﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("MutuallyExclusiveListboxes");

MutuallyExclusiveListboxes.MELBehavior = function(element) {
    MutuallyExclusiveListboxes.MELBehavior.initializeBase(this, [element]);
    this._btnRight = null;
    this._btnLeft = null;
    this._listboxB = null;
    this._listboxA = null;
    
    this._btnRightClickHandler = null;
    this._btnLeftClickHandler = null;
    
}

MutuallyExclusiveListboxes.MELBehavior.prototype = {
    initialize: function() {
        MutuallyExclusiveListboxes.MELBehavior.callBaseMethod(this, 'initialize');
        
        this._listboxA = this.get_element();
        this._btnRightClickHandler = Function.createDelegate(this, this._btnRightClick);
        this._btnLeftClickHandler = Function.createDelegate(this, this._btnLeftClick);
    
        if (this._btnRight) {
            $addHandler(this._btnRight, 'click', this._btnRightClickHandler);
        }
        
        if(this._listboxA) {
            this._sortSelect(this._listboxA);
            $addHandler(this._listboxA, 'dblclick', this._btnRightClickHandler);
        }
        
        if (this._btnLeft) {
            $addHandler(this._btnLeft, 'click', this._btnLeftClickHandler);
        }
        
        if(this._listboxB) {
            this._sortSelect(this._listboxB);
            $addHandler(this._listboxB, 'dblclick', this._btnLeftClickHandler);
        }
        
        
    },
    dispose: function() {        
        if (this._btnRight) {
            $removeHandler(this._btnRight, 'click', this._btnRightClickHandler);
        }
        
        if (this._listboxA) {
            $removeHandler(this._listboxA, 'dblclick', this._btnRightClickHandler);
        }
        
        if (this._btnLeft) {
            $removeHandler(this._btnLeft, 'click', this._btnLeftClickHandler);
        }
        
        if (this._listboxB) {
            $removeHandler(this._listboxB, 'dblclick', this._btnLeftClickHandler);
        }
        
        
        MutuallyExclusiveListboxes.MELBehavior.callBaseMethod(this, 'dispose');
    },
    
    get_btnRight : function() {
        return this._btnRight;
    },
    set_btnRight : function(value) {
        
        if (this._btnRight != value) {
            if (this._btnRight && this.get_isInitialized()) {
                $removeHandler(this._btnRight, 'click', this._btnRightClickHandler);
            }
            this._btnRight = value;
            if (this._btnRight && this.get_isInitialized()) {
                $addHandler(this._btnRight, 'click', this._btnRightClickHandler);
            }
            this.raisePropertyChanged('btnRight');
        }
        
    },
    
    get_btnLeft : function() {
        return this._btnLeft;
    },
    set_btnLeft : function(value) {
        if (this._btnLeft != value) {
            if (this._btnLeft && this.get_isInitialized()) {
                $removeHandler(this._btnLeft, 'click', this._btnLeftClickHandler);
            }
            this._btnLeft = value;
            if (this._btnLeft && this.get_isInitialized()) {
                $addHandler(this._btnLeft, 'click', this._btnLeftClickHandler);
            }
            this.raisePropertyChanged('btnLeft');
        }
    },
    
    get_listboxB : function() {
        return this._listboxB;
    },
    set_listboxB : function(value) {
        if(this._listboxB != value) {
            if (this._listboxB && this.get_isInitialized()) {
                $removeHandler(this._listboxB, 'dblclick', this._btnLeftClickHandler);
            }
            this._listboxB = value;
            if(this._listboxB && this.get_isInitialized()) {
                $addHandler(this._listboxB, 'dblclick', this._btnLeftClickHandler);
            }
            this.raisePropertyChanged('listboxB');
        }
    },
    
    _btnRightClick : function(e) {
        e.preventDefault();
        e.stopPropagation();
        this._moveSelectedOptions(this._listboxA, this._listboxB);
        this._sortSelect(this._listboxA);
        this._sortSelect(this._listboxB);
    },
    
    _btnLeftClick : function(e) {
        e.preventDefault();
        e.stopPropagation();
        this._moveSelectedOptions(this._listboxB, this._listboxA);
        this._sortSelect(this._listboxA);
        this._sortSelect(this._listboxB);
    },
    
    
    //Useful listbox functions courteous Matt Kruse <matt@mattkruse.com> http://www.mattkruse.com/
    _hasOptions : function (obj){if(obj!=null && obj.options!=null){return true;}return false;},
    _selectUnselectMatchingOptions : function (obj,regex,which,only){if(window.RegExp){if(which == "select"){var selected1=true;var selected2=false;}else if(which == "unselect"){var selected1=false;var selected2=true;}else{return;}var re = new RegExp(regex);if(!this._hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){if(re.test(obj.options[i].text)){obj.options[i].selected = selected1;}else{if(only == true){obj.options[i].selected = selected2;}}}}},
    _selectMatchingOptions : function (obj,regex){this._selectUnselectMatchingOptions(obj,regex,"select",false);},
    _selectOnlyMatchingOptions : function (obj,regex){this._selectUnselectMatchingOptions(obj,regex,"select",true);},
    _unSelectMatchingOptions : function (obj,regex){this._selectUnselectMatchingOptions(obj,regex,"unselect",false);},
    _sortSelect : function (obj){var o = new Array();if(!this._hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;}if(o.length==0){return;}o = o.sort(function(a,b){if((a.text+"") <(b.text+"")){return -1;}if((a.text+"") >(b.text+"")){return 1;}return 0;});for(var i=0;i<o.length;i++){obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);}},
    _selectAllOptions : function (obj){if(!this._hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){obj.options[i].selected = true;}},
    
    _moveSelectedOptions : function(from,to) {
        /// <summary>
        /// modified from original to accommodate xlistbox
        /// </summmary>
        
    
        // Unselect matching options, if required
        if (arguments.length > 3) {
            var regex = arguments[3];
            if (regex != "") {
                this._unSelectMatchingOptions(from,regex);
            }
        }
        // Move them over
        if (!this._hasOptions(from)) { return; }
        for (var i=0; i < from.options.length; i++) {
            var o = from.options[i];
            if (o.selected) {
                if (!this._hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
                to.options[index] = new Option( o.text, o.value, false, false);
                AddListItem(to, o.text, o.value); //xlistbox helper function to add to hidden field
            }
        }
        // Delete them from original
        for (var i=(from.options.length-1); i >= 0; i--) {
            var o = from.options[i];
            if (o.selected) {
                RemoveListItem(from, from.options[i].value);  //xlistbox helper function to add to hidden field
                from.options[i] = null;
            }
        }
        if ((arguments.length > 3) || (arguments[2]==true)) {
            this._sortSelect(from);
            this._sortSelect(to);
        }
        from.selectedIndex = -1;
        to.selectedIndex = -1;
    }, 
    _copySelectedOptions : function (from,to){var options = new Object();if(this._hasOptions(to)){for(var i=0;i<to.options.length;i++){options[to.options[i].value] = to.options[i].text;}}if(!this._hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){if(!this._hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}}if((arguments.length<3) ||(arguments[2]==true)){this._sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;},
    _moveAllOptions : function (from,to){this._selectAllOptions(from);if(arguments.length==2){this._moveSelectedOptions(from,to);}else if(arguments.length==3){this._moveSelectedOptions(from,to,arguments[2]);}else if(arguments.length==4){this._moveSelectedOptions(from,to,arguments[2],arguments[3]);}},
    _copyAllOptions : function (from,to){this._selectAllOptions(from);if(arguments.length==2){this._copySelectedOptions(from,to);}else if(arguments.length==3){this._copySelectedOptions(from,to,arguments[2]);}},
    _swapOptions : function (obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;},
    _moveOptionUp : function (obj){if(!this._hasOptions(obj)){return;}for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){this._swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}},
    _removeSelectedOptions : function (from){if(!this._hasOptions(from)){return;}if(from.type=="select-one"){from.options[from.selectedIndex] = null;}else{for(var i=(from.options.length-1);i>=0;i--){var o=from.options[i];if(o.selected){from.options[i] = null;}}}from.selectedIndex = -1;},
    _removeAllOptions : function (from){if(!this._hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){from.options[i] = null;}from.selectedIndex = -1;},
    _addOption : function (obj,text,value,selected){if(obj!=null && obj.options!=null){obj.options[obj.options.length] = new Option(text, value, false, selected);}}
    //End listbox functions
}
MutuallyExclusiveListboxes.MELBehavior.registerClass('MutuallyExclusiveListboxes.MELBehavior', Sys.UI.Behavior);



if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();